package com.cloud.consumer;

import com.alibaba.fastjson.JSONObject;
import com.cloud.entity.CloudLogRecord;
import com.cloud.entity.SysLog;
import com.cloud.service.SyncCloudLogRecordService;
import com.cloud.service.SyncSysLogService;
import com.github.pagehelper.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(topic = "cloud_log", consumerGroup = "log_consumer")
public class ConsumerMqListener implements RocketMQListener<SysLog> {
    @Autowired
    private SyncCloudLogRecordService syncCloudLogRecordService;
    @Autowired
    private SyncSysLogService syncSysLogService;

    private String jsonFlag = "{";
    private String dataKey = "data";
    private Integer dataMaxLength = 1000;

    public void onMessage(SysLog msg) {
        handlerCloudLogRecord(JSONObject.toJSONString(msg));
        handlerSyslogRecord(JSONObject.toJSONString(msg));
    }

    public void handlerCloudLogRecord(String text) {
        SysLog sysLog = JSONObject.parseObject(text, SysLog.class);

//        String reqParam = sysLog.getReqParam();
//        if (!StringUtil.isEmpty(reqParam) && reqParam.length() >= dataMaxLength) {
//            reqParam = reqParam.substring(0, dataMaxLength);
//        }

        CloudLogRecord cloudLogRecord = new CloudLogRecord()
//                    .setId()
                .setReqUser(sysLog.getReqUser())
                .setReqAddr(sysLog.getReqAddr())
                .setAcctType(sysLog.getAcctType())
                .setReqMethod(sysLog.getReqMethod())
//                .setReqParam(reqParam)
                .setSourceApplicationName(sysLog.getSourceApplicationName())
                .setTargetApplicationName(sysLog.getApplicationName())
                .setReqTime(sysLog.getReqTime())
                .setSkyTraceId(sysLog.getSkyTraceId())
                .setTraceId(sysLog.getTraceId());

        syncCloudLogRecordService.receiveLogRecord(cloudLogRecord);
    }

    public void handlerSyslogRecord(String text) {
//        log.info("当前线程号：" + Thread.currentThread().getName());
        SysLog sysLog = JSONObject.parseObject(text, SysLog.class);
        String respBody = sysLog.getRespBody();
        if (!StringUtil.isEmpty(respBody) && respBody.indexOf(jsonFlag) == 0) {
            JSONObject json = JSONObject.parseObject(respBody);
            if (json.containsKey(dataKey)) {
                String dataValue = json.getString(dataKey);
                if (!StringUtil.isEmpty(dataValue) && dataValue.length() >= dataMaxLength) {
                    json.put(dataKey, json.getString(dataKey).substring(0, dataMaxLength));
                    sysLog.setRespBody(json.toJSONString());
                }
            }
            //如果返回的数据不规范，直接取1000就好
            else if (respBody.length() >= dataMaxLength) {
                sysLog.setRespBody(respBody.substring(0, dataMaxLength));
            }
        } else if (!StringUtil.isEmpty(respBody) && respBody.length() >= dataMaxLength) {
            sysLog.setRespBody(respBody.substring(0, dataMaxLength));
        }
        String reqParam = sysLog.getReqParam();
        if (!StringUtil.isEmpty(reqParam) && reqParam.length() >= dataMaxLength) {
            sysLog.setReqParam(reqParam.substring(0, dataMaxLength));
        }

        syncSysLogService.receiveLogRecord(sysLog);
    }
}
